<template>
	<u-toast ref="uToastRef"></u-toast>
	<view class="login">
		<image class="bg" src="/static/images/login_bg.png" />
		<view class="box">
			<view class="case">
				<view class="logo">
					<image src="/static/images/logo.png" />
				</view>
				<view class="title">中储粮质检中心</view>
				<view class="form">
					<u--form labelPosition="left" :model="model.dataForm" :rules="model.rules" ref="loginFormRef">
						<u-form-item label="" prop="phone" labelWidth="0" ref="phoneRef">
							<view class="form_item">
								<view class="form_label"><span class="required">*</span>手机号</view>
								<view class="form_value">
									<u-input v-model="model.dataForm.phone" border="none" :customStyle="customStyle"
										placeholderClass="placeholder" placeholder="请输入手机号"></u-input>
								</view>
							</view>
						</u-form-item>
						<u-form-item label="" prop="password" labelWidth="0">
							<view class="form_item">
								<view class="form_label"><span class="required">*</span>密码</view>
								<view class="form_value">
									<u-input v-model="model.dataForm.password" border="none" :customStyle="customStyle"
										placeholderClass="placeholder" placeholder="请输入密码"></u-input>
								</view>
							</view>
						</u-form-item>
						<u-form-item label="" prop="repeatPwd" labelWidth="0">
							<view class="form_item">
								<view class="form_label"><span class="required">*</span>重复密码</view>
								<view class="form_value">
									<u-input v-model="model.dataForm.repeatPwd" border="none" :customStyle="customStyle"
										placeholderClass="placeholder" placeholder="请再次输入密码"></u-input>
								</view>
							</view>
						</u-form-item>
						<u-form-item label="" prop="smscode" labelWidth="0">
							<view class="form_item">
								<view class="form_label"><span class="required">*</span>验证码</view>
								<view class="form_value">
									<u-input v-model="model.dataForm.smscode" type="number" border="none"
										:customStyle="customStyle" placeholderClass="placeholder" placeholder="请输入验证码">
										<template #suffix>
											<up-code ref="uCodeRef" @change="onCodeChange" seconds="60"
												changeText="X秒重新获取"></up-code>
											<up-button @tap="getCode" :text="tips" size="mini"
												color="#079152"></up-button>
										</template>
									</u-input>
								</view>
							</view>
						</u-form-item>
					</u--form>
				</view>
				<view class="tabs">
					<span class="label"></span>
					<span class="label" @click="toLogin">已有账号，去登录</span>
				</view>
			</view>
			<view class="btn">
				<up-button type="primary" color="#079152" shape="circle" text="注册" @click="onRegister"></up-button>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, reactive } from "vue";
	import { Register, GetCode } from "@/api/login.js";

	const uToastRef = ref();
	const loginFormRef = ref();
	const phoneRef = ref();

	// 注册信息
	const model = reactive<any>({
		dataForm: {
			password: "",
			repeatPwd: "",
			smscode: "",
			phone: "",
			tenantId: "2",
		},
		rules: {
			phone: [
				{
					type: "string",
					required: true,
					message: "请输入手机号",
					trigger: ["change"],
				},
				{
					validator: (rule : any, value : any, callback : any) => {
						return uni.$u.test.mobile(value);
					},
					message: "请输入正确的手机号",
					trigger: ["change"],
				},
			],
			password: {
				type: "string",
				required: true,
				message: "请输入密码",
				trigger: ["change"],
			},
			repeatPwd: [
				{
					type: "string",
					required: true,
					message: "请再次输入密码",
					trigger: ["change"],
				},
				{
					validator: (rule : any, value : any, callback : any) => {
						return value == model.dataForm.password;
					},
					message: "输入的两次密码不一致",
					trigger: ["change"],
				},
			],
			smscode: {
				type: "string",
				required: true,
				len: 6,
				message: "请输入6位验证码",
				trigger: ["change"],
			},
		},
	});
	// 输入框样式
	const customStyle = ref<any>({
		background: "#F6F6F7",
		height: "80rpx",
		paddingLeft: "14px",
		paddingRight: "14px",
	});

	const tips = ref("发送");
	const uCodeRef = ref(null);

	const onCodeChange = (text : any) => {
		tips.value = text;
	};
	const getCode = () => {
		loginFormRef.value.validateField('phone', async (val : any) => {
			if (val.length == 0) {
				if (uCodeRef.value.canGetCode) {
					uni.showLoading({
						title: "正在获取验证码",
					});
					try {
						await GetCode({ mobile: model.dataForm.phone });
						uni.hideLoading();
						uToastRef.value.show({
							message: "验证码已发送",
							position: 'top',
							type: "success",
							complete() {
								uCodeRef.value.start();
							},
						});
					} catch (e) {
						uni.hideLoading();
					}
				} else {
					uToastRef.value.show({
						message: "倒计时结束后再发送",
						position: 'top',
						type: "warning",
					});
				}
			}
		})


	};

	const onRegister = () => {
		loginFormRef.value.validate().then(async () => {
			await Register({ ...model.dataForm, username: model.dataForm.phone });
			uToastRef.value.show({
				message: "注册成功",
				type: "success",
				position: 'top',
				complete() {
					uni.navigateBack()
				},
			});
		});
	};

	const toLogin = () => {
		uni.navigateTo({
			url: "/pages/login/login",
		});
	};
</script>

<style scoped lang="scss">
	::v-deep.placeholder {
		color: #ccc;
	}

	::v-deep.u-form-item__body__right__message {
		text-align: right;
	}

	::v-deep.u-form-item__body {
		padding: 5px 0 !important;
	}

	.login {
		height: 100vh;
		background: #e7f1ee;
		position: relative;

		.bg {
			width: 100%;
			height: 460rpx;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
		}

		.box {
			position: absolute;
			top: 20%;
			padding: 0 20rpx;
			width: 100%;
			box-sizing: border-box;

			.case {
				background: #fff;
				border-radius: 10px;
				position: relative;
				padding: 80rpx 40rpx 20rpx 40rpx;

				.logo {
					position: absolute;
					top: -54rpx;
					left: 50%;
					transform: translateX(-50%);
					background: #fff;
					border-radius: 50%;
					padding: 20rpx;

					image {
						width: 82rpx;
						height: 82rpx;
					}
				}

				.title {
					font-weight: 700;
					font-size: 36rpx;
					color: #353535;
					text-align: center;
					margin-bottom: 5rpx;
				}

				.form {
					margin-top: 50rpx;

					.form_item {
						//   margin-bottom: 30rpx;

						.form_label {
							font-size: 26rpx;
							color: #353535;
							font-weight: 500;
							margin-bottom: 10rpx;

							.required {
								color: red;
								margin-right: 2rpx;
							}
						}
					}
				}

				.tabs {
					display: flex;
					justify-content: space-between;
					align-items: center;
					color: #079152;
					font-size: 28rpx;
					margin-top: 40rpx;
				}
			}

			.btn {
				margin-top: 100rpx;
			}

			.tips {
				font-size: 14px;
				margin-top: 20rpx;
				color: #9e9e9e;

				text {
					color: #079152;
				}
			}
		}
	}
</style>